#include<stdio.h>
#include<math.h>
#include<string.h>

void InputArr(int arr[], int *n)
{
    printf(" - Enter a number of items(>1): ");
    scanf("%d", &(*n));
    while( (*n) < 2 )
    {
        printf(" - Your input invalid, re-enter a number of items: ");
        scanf("%d", &(*n));
    }

    int i;
    for (i=0; i<(*n); i++)
    {
        printf(" --> Enter item %d: ", i);
        scanf("%d", &arr[i]);
        while (ConfirmArr(arr,i))
        {
            printf(" --> Exists the same number, re-enter item %d: ", i);
            scanf("%d", &arr[i]);
        }

    }
}

int ConfirmArr(int arr[], int n)
{
    int i;
    for (i=0; i<n; i++)
        if ( arr[i] == arr[n] )
        return (1);
    return (0);
}

void OutputArr(int arr[], int n)
{
    int i;
    for (i=0; i<n; i++)
    {
        printf("%d  ", arr[i]);
    }
    printf("\n");
}

int GetSum(int arr[], int n)
{
    int i,sum = 0;
    for (i=0; i<n; i++)
        sum += arr[i];
    return(sum);
}

int GetSumOf(int arr[], int n, int Confirm(int x))
{
    int i,sum = 0;
    for ( i=0; i<n; i++ )
        if ( Confirm(arr[i]) == 1 )
        {
            //printf("%d ", arr[i]);
            sum += arr[i];
        }
    return(sum);
}

int IsPrimeNum(num)
{
    if (num < 2)return (0);
    else if ((num==2)||(num==3))
        return(1);
    else if (( num%2 == 0 ) || ( num %3 == 0))
        return(0);
    else
    {
        int i=5;
        while ( i <= sqrt(num) )
        {
            if((num%i == 0) || (num%i+2 == 0))
                return (0);
            i+=6;
        }
        return (1);
    }
}

int IsPerfectNum(num)
{
    int i,sum=0;
    for (i=2; i<= (num/2); i++)
    {
        if ((num % i)==0)
        {
            sum += i;
        }
    }

    if((sum+1) == num)
        return(1);
    else
        return(0);
}

int ItemEqualX(int arr[], int n, int x)
{
    int i;
    for (i=0; i<n; i++)
    {
        if (arr[i] == x)
        {
            //printf("item equal = %d (%d)\n", i, arr[i]);
            return (i);
        }

    }
    //printf("return 0 no item equal\n");
    return(-1);
}

int SecondMinimize(int arr[], int n)
{
    int i, fstMin, scndMin;
    if (arr[0]<arr[1])
    {
        fstMin = arr[0];
        scndMin = arr[1];
    }
    else
    {
        fstMin = arr[1];
        scndMin = arr[0];
    }

    for (i=2; i<n; i++)
    {

        if (arr[i] < fstMin)
        {
            scndMin = fstMin;
            fstMin = arr[i];
        }
        else if (arr[i] < scndMin)
        {
            scndMin = arr[i];
        }

    }
    return (scndMin);

}

int SumOfPair(int arr[], int n, int x, int *a, int *b)
{
    int i, j;
    for (i=0; i<n-1; i++)
        for (j= i+1; j<n; j++)
        {
            if ((arr[i] + arr[j]) == x )
            {
                *a = arr[i];
                *b = arr[j];
                return (1);
            }
        }
    return (0);
}

int RmvItemByIndex(int arr[], int *n, int index)
{
    if ((index < 0) || (index >= *n))
    {
        //printf("return 0 index <0 or >n %d\n", index);
        return(0);
    }

    int i;
    for (i= index; i <(*n - 1); i++)
    {
        arr[i] = arr[i+1];
    }
    (*n)--;
    return (1);
}

int RmvItemByValue(int arr[], int *n, int value)
{
    int indexOfValue;
    indexOfValue = ItemEqualX(arr, *n, value);
    //printf("Index of value = %d", indexOfValue);
    if (indexOfValue == -1)
    {
        //printf(" - return 0, Index of value = %d\n", indexOfValue);
        return(0);
    }

    else
    {
        RmvItemByIndex(arr, &(*n), indexOfValue);
    }
    return (1);

}

int SelectFunc()
{
    // loop
    int func;
    printf(" - Select functions you want to do (1-8):");
    scanf("%d", &func);
    while (( func < 1 )||( func >8 ))
    {
        printf(" - Your input invalid, re-select functions you want to do (1-8):");
        scanf("%d", &func);
    }
    return (func);
}

void SwitchFunc(int arr[], int *n, int func, int *theFirstOutputOfPair, int *theSecondOutputOfPair, int *individualOutput)
{
    switch(func)
    {
    case 1:
    {
        *individualOutput = GetSum(arr, (*n));
        printf(" - 1. SUM of all numbers: %d\n", *individualOutput);
        break;
    }
    case 2:
    {
        *individualOutput = GetSumOf(arr, *n, IsPrimeNum);
        printf(" - 2. SUM of prime numbers: %d\n", *individualOutput);
        break;
    }
    case 3:
    {
        *individualOutput = GetSumOf(arr, *n, IsPerfectNum);
        printf(" - 3. SUM of perfect numbers: %d\n", *individualOutput);
        break;
    }
    case 4:
    {
        printf(" - 4. Item equal x\n");
        printf(" + Input x: ");
        int temp;
        scanf("%d", &temp);
        *individualOutput = ItemEqualX(arr, *n, temp);
        printf(" + Item equal x: %d\n", (*individualOutput));
        break;
    }
    case 5:
    {
        *individualOutput = SecondMinimize(arr, *n);
        printf(" - 5. The second minimize number in array: %d\n", *individualOutput);
        break;
    }
    case 6:
    {

        printf(" - 6. A pair has SUM equal x: \n");
        printf(" + Input x: ");
        //int c;
        scanf("%d", &(*individualOutput));
        SumOfPair(arr, *n, *individualOutput, &(*theFirstOutputOfPair), &(*theSecondOutputOfPair));
        printf(" + A pair has SUM equal %d is %d and %d \n", *individualOutput, *theFirstOutputOfPair, *theSecondOutputOfPair);
        break;
    }
    case 7:
    {
        printf(" - 7. Remove item by index/n");
        printf(" + Input index: ");
        int temp;
        scanf("%d", &temp);
        RmvItemByIndex(arr, &(*n), temp);
        OutputArr(arr, (*n));
        break;
    }
    case 8:
    {
        printf(" - 8. Remove item by value\n");
        printf(" + Input value: ");
        int temp;
        scanf("%d", &temp);
        if ( RmvItemByValue(arr, &(*n), temp) == -1 )
        {
            printf(" - There isn't any number equal %d\n", temp);
        }
        else
            OutputArr(arr, (*n));
        break;
    }
    }
}

int ConfirmText(char firstText[], char secondText[])
{
    char confirm;
    printf("%s", firstText);
    fflush(stdin);
    scanf("%c", &confirm);
    while((confirm!='y')&&(confirm!='n')&&(confirm!='Y')&&(confirm!='N'))
    {
        printf("%s",secondText);
        fflush(stdin);
        scanf("%c", &confirm);
    }
    if ((confirm=='y')||(confirm=='Y'))
        return 1;
    else return 0;
}

void WriteToFile(int arr[], int n, int func, int theFirstOutputOfPair, int theSecondOutputOfPair, int individualOutput)
{
    char fileName[50], location[100]="c:\\test\\";
    printf(" - Enter file name (*.txt):");
    scanf("%s", &fileName);
    strcat(location, fileName);
    strcat(location, ".txt");
    printf(" - Location: %s\n", location);
    FILE *fp;
    fp = fopen(location,"w");

    int i;
    for (i=0; i<n; i++)
    {
        fprintf(fp,"%d  ", arr[i]);
    }

    fprintf(fp,"\n");

    switch(func)
    {
    case 1:
        {
            fprintf(fp, " - 1. SUM of all numbers: %d\n", individualOutput);
            break;
        }
    case 2:
        {
            fprintf(fp, " - 2. SUM of prime numbers: %d\n", individualOutput);
            break;
        }
    case 3:
        {
            fprintf(fp, " - 3. SUM of perfect numbers: %d\n", individualOutput);
            break;
        }
    case 4:
        {
            fprintf(fp, " - 4. Item equal x: %d\n", individualOutput);
            break;
        }
    case 5:
        {
            fprintf(fp, " - 5. The second minimize number in array: %d\n", individualOutput);
            break;
        }
    case 6:
        {
            fprintf(fp, " + A pair has SUM equal %d is %d and %d \n", individualOutput, theFirstOutputOfPair, theSecondOutputOfPair);
            break;
        }
    case 7:
        {
            fprintf(fp, " - 7. Remove item by index\n");
            break;
        }
    case 8:
        {
            fprintf(fp, " - 8. Remove item by value\n");
            break;
        }
    }
    fclose(fp);
    printf(" --> Write file successfully! \n");
}

void Display()
{
    do
    {
        int arr[30], n, theFirstOutputOfPair, theSecondOutputOfPair, individualOutput, func;
        printf("WELCOME TO ARRAY HELL!!!\n");
        printf("    * List function:\n1. Get SUM of all item\n2. Get SUM of primes item\n3. Get SUM of perfect item\n4. Find item equal x\n5. Find the second minimize number in array\n6. Find a pair has SUM equal x\n7. Remove item by index\n8. Remove item by value\n");
        func = SelectFunc();
        InputArr(arr,&n);
        printf(" - Your array with %d items :\n", n);
        OutputArr(arr,n);
        SwitchFunc(arr, &n, func, &theFirstOutputOfPair, &theSecondOutputOfPair, &individualOutput);
        if(ConfirmText("- Do you want to save result to file (y/n)?", "- Your input invalid, do you want to save result to file (y/n)?"))
        {
            WriteToFile(arr, n, func, theFirstOutputOfPair, theSecondOutputOfPair, individualOutput);
        }
    }
    while (ConfirmText("Do you want to do again (y/n)?", "Your input invalid, do you want to do again (y/n)?"));
}

void main()
{
    Display();
}
